package org.server;




import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public abstract class QFileHandler {
	
	/**
	 * <b>getFileString</b><br>
	 * Method used to return the string in a certain file
	 * @param FilePath the path of hat file
	 * @return string in that file
	 */
	public static StringBuffer getFileString(String FilePath)
	{
		StringBuffer sbuff=new StringBuffer();
		File sourceFile = new File(FilePath);
		try
		{
			
			 File f=new File(sourceFile.getAbsoluteFile().toString());
			 FileReader input=new FileReader(f);
			 BufferedReader buff=new BufferedReader(input);
			 int ch;
			 while((ch=buff.read())!=-1)
			 {
				 sbuff.append((char)ch);
			 }
			 buff.close();
			 //FileString = sbuff.toString();
		}
		catch(Exception x){}
		return sbuff;
	}
	
	/**
	 * <b>writeToFile</b><br>
	 * Method used to write certain text to a certain file
	 * @param FileToWrite the path of the file to write
	 * @param StrToWrite the string to write in the file
	 */
	public static void writeToFile(String FileToWrite,String StrToWrite)
	{		
		try {
            
            BufferedWriter outBuff = new BufferedWriter(new FileWriter(FileToWrite));
            outBuff.write(StrToWrite);
            outBuff.flush();
            outBuff.close();
        } 
        catch (IOException e) {
            System.out.println("IOException:");
            e.printStackTrace();
        }
	}
	public static  String getFileName(String FilePath)
	{
		int iStart = FilePath.lastIndexOf(File.separatorChar);
		int iEnd = FilePath.lastIndexOf('.');
		
		return FilePath.substring(iStart + 1,iEnd);
	}
	public static String getFileExtension(String FilePath)
	{
		int iStart = FilePath.lastIndexOf('.');
		return FilePath.substring(iStart+1);
	}
	public static String getParentPath(String FilePath)
	{
		int iEnd = FilePath.lastIndexOf(File.separatorChar);
		return FilePath.substring(0,iEnd + 1);
	}
}
